package zj.it.bhne.gridengineeringsurvey.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.flyco.dialog.widget.NormalDialog;

import java.util.List;

import zj.it.bhne.gridengineeringsurvey.MyApplication;
import zj.it.bhne.gridengineeringsurvey.R;
import zj.it.bhne.gridengineeringsurvey.entity.greendao.Items;
import zj.it.bhne.gridengineeringsurvey.entity.greendao.Online;
import zj.it.bhne.gridengineeringsurvey.myutils.MyUtils;
import zj.it.bhne.gridengineeringsurvey.myutils.ThreadManager;

import static zj.it.bhne.gridengineeringsurvey.myutils.ThreadManager.getThreadPool;

/**
 * BottomActivity中的Twofragment中的在线项目的适配器
 */
public class MainsAdapter extends BaseAdapter {
    private Context context;
    private List<Online> list;
    private Handler handler = new Handler();


    public MainsAdapter(Context context, List<Online> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"SetTextI18n", "InflateParams"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final VieHolder vieHolder;
        if (convertView == null) {
            vieHolder = new VieHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_list, null);
            vieHolder.one = convertView.findViewById(R.id.one);
            vieHolder.two = convertView.findViewById(R.id.two);
            vieHolder.three = convertView.findViewById(R.id.three);
            vieHolder.imageButton = convertView.findViewById(R.id.online_ib_down);
            convertView.setTag(vieHolder);
        } else
            vieHolder = (VieHolder) convertView.getTag();
        final Online items = list.get(position);
        vieHolder.one.setText(items.getDescription() + "");
        vieHolder.two.setText(items.getId() + " ");
        vieHolder.three.setText(items.getCreatetime());
        if (items.getDown().equals("yes")) {
            vieHolder.imageButton.setText("已下载");
        } else {
            vieHolder.imageButton.setText("下载");
        }

        //vieHolder.imageButton.setCurrentText("下载");
        vieHolder.imageButton.setOnClickListener(v -> {
            if ("已下载".equals(vieHolder.imageButton.getText().toString())) {
                vieHolder.imageButton.setEnabled(false);

                final NormalDialog dialog = new NormalDialog(context);
                dialog.content("已下载过本项目，是否继续下载?")//
                        .style(NormalDialog.STYLE_TWO)//
                        .btnNum(2)
                        .title("下载设置")
                        .btnText("下载", "取消")//
                        .show();
                dialog.setOnBtnClickL(
                        () -> {
                            dialog.dismiss();
                            setDown(vieHolder,items);
                        }, dialog::dismiss);

                MyUtils.getToast(context, "已经下载完成，不可重复下载");
            } else {
                vieHolder.imageButton.setEnabled(true);
                setDown(vieHolder,items);

            }


        });
        return convertView;
    }

    /**
     *
     * @param vieHolder v
     * @param items 临时保存
     */
    private void setDown(VieHolder vieHolder, Online items) {
        items.setDown("yes");
        MyApplication.getInstances().getOnlineDao().update(items);
        final Items item = new Items();
        vieHolder.imageButton.setText("下载中");
        item.setId(null);
        item.setName(items.getDescription());
        item.setNote(items.getPhr());
        item.setTime(items.getCreatetime());
        item.setIsCheck(false);
        item.setState("在线");
        item.setStop("0");
        item.setVoltage(items.getFzr());
        item.setType(items.getId() + "");
        item.setProjectId(items.getId());
        Runnable runnable = () -> {
            if (MyApplication.getInstances().getItemsDao().insert(item) > 0) {
                handler.postDelayed(() -> vieHolder.imageButton.setText("已下载"), 1500);
            } else {
                MyUtils.getToast(context, "创建失败");
            }
        };
        ThreadManager.ThreadPool threadPool = getThreadPool();
        threadPool.execute(runnable);
    }

    private class VieHolder {
        private TextView one, two, three;
        private Button imageButton;
    }

}
